package jsp.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.entities.Employee;
import jpa.entities.Project;
import jsp.classes.Session;

/**
 * @author Remco Haszing
 * @author Aymeric Blok
 */
public class NewProject extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Session session = (Session) request.getSession(true).getAttribute("user");
            Employee e = null;
            String title = null;
            String[] deadline = null;
            List<Employee> users = session.getJPAConnection().getAllUsers();
            for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
                String k = entry.getKey();
                String[] v = entry.getValue();
                if (k.equals("projectName")) {
                    title = v[0];
                } else if (k.equals("deadline")) {
                    deadline = v[0].split("-");
                } else if (k.equals("owner")) {
                    e = session.findEmployeeById(Long.parseLong(v[0]));
                }
            }
            for (String i : deadline) {
                System.out.println(i);
            }
            session.getJPAConnection().create(new Project(title, e, new GregorianCalendar(), new GregorianCalendar(Integer.parseInt(deadline[0]), Integer.parseInt(deadline[1]) - 1, Integer.parseInt(deadline[2])), new ArrayList<Employee>()));
            session.getJPAConnection().edit(e);
            response.getWriter().print("true");
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().print("false");
        }
    }
}
